var rating = (function() {

	// 继承
	var extend = function(parentClass, childClass) {
		var F = function() {};
		F.prototype = parentClass.prototype;
		childClass.prototype = new F();
	}

	//点亮星星的默认模板
	var Light = function(el, options) {
		this.$el = $(el);
		this.$item = this.$el.find(options.child);
		this.opts = options,
			this.add = 1,
			this.selectEvent = 'mouseover';
	};

	Light.prototype.lightOn = function(num) {
		this.$item.each(function(index) {
			if (index >= num) {
				$(this).css("background-position", "0 top")
			} else {
				$(this).css("background-position", "-1px center")
			}
		})
	};

	Light.prototype.bindEvent = function() {
		var self = this,
			length = self.$item.length;
		self.$el.on(self.selectEvent, self.opts.child, function(e) {
			var $this = $(this),
				num = 0;
			self.select(e, $this);
			num = $this.index() + self.add;
			self.lightOn(num);
			(typeof self.opts.select === 'function') && self.opts.select.call(this, num, length);

		}).on('click', self.opts.child, function() {
			self.opts.num = $(this).index() + self.add;
			(typeof self.opts.chosen === 'function') && self.opts.chosen.call(this, self.opts.num, length);
		}).on('mouseleave', function() {
			self.lightOn(self.opts.num);
		});
	};

	Light.prototype.init = function(el, options) {
		this.lightOn(this.opts.num);
		if (!this.opts.readyOnly) {
			this.bindEvent();
		}
	};

	Light.prototype.select = function() {
		throw new Error("子类必须重写该select()方法");
	}

	//点亮整颗星星
	var LightAll = function(el, options) {
		Light.call(this, el, options);
		this.selectEvent = 'mouseover';
	};

	extend(Light, LightAll);

	LightAll.prototype.lightOn = function(num) {
		Light.prototype.lightOn.call(this, num);
	};
	LightAll.prototype.select = function() {
		this.add = 1;
	};

	//点亮半颗星星
	var LightHarf = function(el, options) {
		Light.call(this, el, options);
		this.selectEvent = 'mousemove';
	};

	extend(Light, LightHarf);

	LightHarf.prototype.lightOn = function(num) {
		var count = Math.floor(num),
			isHarf = count !== num;
		Light.prototype.lightOn.call(this, count);
		if (isHarf) {
			this.$item.eq(count).css('background-position', "-1px bottom");
		}
	};

	LightHarf.prototype.select = function(e, $this) {
		if (e.pageX - $this.offset().left < $this.width() / 2) {
			this.add = 0.5;
		} else {
			this.add = 1;
		}
		num = $this.index() + self.add;
	}

	// 定义一些默认的参数
	var defaults = {
		type: 'LightAll',
		num: 0,
		readyOnly: false,
		child: '.list-item',
		chosen: function() {},
		select: function() {}
	};

	// 定义一个对象，使得输入的字符串有对应的模式
	var mode = {
		'LightAll': LightAll,
		'LightHarf': LightHarf
	}

	// 初始化
	var init = function(el, option) {
			var options = $.extend({}, defaults, typeof option == 'object' && option);
			if (!mode[options.type]) {
				options.type = 'LightAll';
			}
			// new (options['type'])().init(el,options);
			// console.log(options['type']);
			new mode[options.type](el, options).init();
		}

	// 注册成jQuery插件
	$.fn.extend({
		rating: function(option) {
			this.each(function() {
				init(this, option);
			});
			return this;
		}
	});
	return {
		init: init
	}
})();


/*
*可以传递的选项
*type:选择点亮的模式，半颗或者整颗，默认整颗,参数选项："LightAll和LightHarf"
*num:初始点亮的星星数量，默认为0
*readyOnly：设置是否为只读模式，默认为false
*child:设置需要改变的子元素,默认为 class=list-item
*chosen:设置鼠标滑过后需要执行的函数
*select:设置鼠标点击后需要执行的函数
*/

